# -*- coding:utf-8 -*-
import json
import ssl

import websocket
import _thread
import time


def on_message1(ws, message):
    m = json.loads(message)
    print(m["type"])
    print(m["data"])


def on_error(ws, error):
    print(error)


def on_close(ws, close_status_code, close_msg):
    print("### closed ###")


def on_open(ws):
    print("Opened connection")


if __name__ == "__main__":
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp(
        "wss://zj-cn-live-comet.chat.bilibili.com:2245/sub",
        on_open=on_open,
        on_message=on_message1,
        on_error=on_error,
        on_close=on_close)

    ws.run_forever(ping_interval=30, ping_timeout=3, sslopt={"cert_reqs": ssl.CERT_NONE})

"""
解决报错：
: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)
"""
"""
wss://cn-hnld-ccc-live-tracker-04.chat.bilibili.com/?protocol=10&stream=live_438956793_2246113&roomid=30573546&timeshift=0


Opened connection
{'id': '371807372'}
{'jsonstr': '{ "debug": "web rule matched!", "stunServers": ["stun:stun.chat.bilibili.com:3478", "stun:stun6.chat.bilibili.com:3478"], "natCheckStunServers":["stun:stun-1.chat.bilibili.com:3478", "stun:stun-2.chat.bilibili.com:3478", "stun:stun-3.chat.bilibili.com:3478", "stun:stun-4.chat.bilibili.com:3478"], "natFilteringCheckServers":["120.92.94.40", "120.92.147.75", "120.92.159.253", "120.92.159.60", "120.92.115.159"], "fragmentShareOverride": 1.0, "fragmentShareOverrideMaxBytes": 3500000, "fragmentCountOutdated": 4, "p2pBufferLengthSec": 1.0, "nonP2PFragmentCount": 3, "p2pDelayStopMaxMs": 1000, "p2pSafeBufferDurationForDelayStopMs": 1400, "connectedLimit":20, "enableHideOwner": true, "maxDistance": 1, "candidateWaitMaxTimeMs": 650, "useSeedBox": true, "useSeedBoxMore": true, "seedboxConnectNumBySliceId": 2, "seedScoreDown": 5, "p2pFetchLowestRatio": 0.6, "p2pAddSliceProbability": 0.2, "p2pShareDataObCycleCount": 3, "p2pSliceIdControlShareCheckIntervalMs": 4000, "p2pDeleteSliceProbability": 0.35, "p2pShareLowestRatio": 0.6, "getPeersIntervalMs": 20000, "natDowngradeLimit": 10 }'}

"""
